﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IP.Core.IPData;
using IP.Core.IPUserService;
using WebDS.CDBNames;
using WebDS;
using IP.Core.IPCommon;
using WebUS;

public partial class ChucNang_F615_LoaiHopDongThuocDonViQuanLy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!this.IsPostBack)
            {
                decimal v_dc_id_nhom = CIPConvert.ToDecimal(Session["user_quyen"]);
                if (!PhanQuyenUtils.is_having_quyen_page("F615_LoaiHopDongThuocDonViQuanLy.aspx", v_dc_id_nhom))
                    Response.Redirect("/TRMProject/Default.aspx");
                load_cbo_dv_quan_ly();
                load_lst_loai_hd_chua_add();
                load_lst_loai_hd_da_add();
            }
        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }
    }

    #region Members
    US_CM_DM_TU_DIEN m_us_loai_hd = new US_CM_DM_TU_DIEN();
    DS_CM_DM_TU_DIEN m_ds_loai_hd = new DS_CM_DM_TU_DIEN();
    #endregion

    #region Data Structures

    #endregion

    #region Private Methods
    public string get_mapping_ten_chuc_nang(decimal i_dc_id_chuc_nang)
    {
        try
        {
            string v_str_chuc_nang = "";
            US_HT_CHUC_NANG v_us = new US_HT_CHUC_NANG(i_dc_id_chuc_nang);
            v_str_chuc_nang = v_us.strTEN_CHUC_NANG;
            return v_str_chuc_nang;
        }
        catch (Exception v_e)
        {
            throw v_e;
        }

    }
    private void load_cbo_dv_quan_ly()
    {
        US_CM_DM_TU_DIEN v_us_dv_quan_ly = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_dv_quan_ly = new DS_CM_DM_TU_DIEN();
        v_us_dv_quan_ly.fill_tu_dien_cung_loai_ds(LOAI_TU_DIEN.DON_VI_QUAN_LY_CHINH, v_ds_dv_quan_ly);
        m_cbo_dv_quan_ly.DataSource = v_ds_dv_quan_ly.CM_DM_TU_DIEN;
        m_cbo_dv_quan_ly.DataTextField = CM_DM_TU_DIEN.TEN;
        m_cbo_dv_quan_ly.DataValueField = CM_DM_TU_DIEN.ID;
        m_cbo_dv_quan_ly.DataBind();
    }
    private void load_lst_loai_hd_chua_add()
    {
        US_CM_DM_TU_DIEN v_us_loai_hd = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_loai_hd = new DS_CM_DM_TU_DIEN();
        v_us_loai_hd.FillDataset(v_ds_loai_hd, " WHERE ID_LOAI_TU_DIEN = " + (int)e_loai_tu_dien.LOAI_HOP_DONG + " AND ID NOT IN (SELECT ID_LOAI_HOP_DONG FROM GD_LOAI_HD_DV_QUAN_LY)");
        m_lst_loai_hd_chua_add.DataSource = v_ds_loai_hd.CM_DM_TU_DIEN;
        m_lst_loai_hd_chua_add.DataTextField = CM_DM_TU_DIEN.TEN;
        m_lst_loai_hd_chua_add.DataValueField = CM_DM_TU_DIEN.ID;
        m_lst_loai_hd_chua_add.DataBind();
    }
    private void load_lst_loai_hd_da_add()
    {
        US_CM_DM_TU_DIEN v_us_loai_hd = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_loai_hd = new DS_CM_DM_TU_DIEN();
        v_us_loai_hd.FillDataset(v_ds_loai_hd, " WHERE ID IN (SELECT ID_LOAI_HOP_DONG FROM GD_LOAI_HD_DV_QUAN_LY WHERE ID_DV_QUAN_LY =" + CIPConvert.ToDecimal(m_cbo_dv_quan_ly.SelectedValue) + ")");

        m_lst_loai_hd_da_add.DataSource = v_ds_loai_hd.CM_DM_TU_DIEN;
        m_lst_loai_hd_da_add.DataTextField = CM_DM_TU_DIEN.TEN;
        m_lst_loai_hd_da_add.DataValueField = CM_DM_TU_DIEN.ID;
        m_lst_loai_hd_da_add.DataBind();
    }
    private void update_dv_quan_ly_loai_hd()
    {
        try
        {
            m_lbl_mess.Text = "";
            string v_str_id_loai_hds = "";
            foreach (ListItem ltTemp in this.m_lst_loai_hd_da_add.Items)
            {

                v_str_id_loai_hds += ltTemp.Value + ",";
            }
            US_V_GD_LOAI_HD_DV_QUAN_LY v_us_dv_quan_ly_loai_hd = new US_V_GD_LOAI_HD_DV_QUAN_LY();
            v_us_dv_quan_ly_loai_hd.update_dv_quan_ly_loai_hd(CIPConvert.ToDecimal(m_cbo_dv_quan_ly.SelectedValue), v_str_id_loai_hds);
            m_lbl_mess.Text = "Cập nhật đơn vị quản lý cho loại hợp đồng thành công";
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình cập nhật bản ghi.";
            throw v_e;
        }
    }
    #endregion

    //
    //
    // events
    //
    //

    protected void m_cmd_right_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            while (m_lst_loai_hd_chua_add.Items.Count > 0 && m_lst_loai_hd_chua_add.SelectedItem != null)
            {
                ListItem selectedItem = m_lst_loai_hd_chua_add.SelectedItem;
                selectedItem.Selected = false;
                m_lst_loai_hd_da_add.Items.Add(selectedItem);
                m_lst_loai_hd_chua_add.Items.Remove(selectedItem);
            }

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }

    }
    protected void m_cmd_right_all_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            foreach (ListItem ltTemp in this.m_lst_loai_hd_chua_add.Items)
            {
                this.m_lst_loai_hd_da_add.Items.Add(ltTemp);
            }
            this.m_lst_loai_hd_chua_add.Items.Clear();


        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }

    }
    protected void m_cmd_left_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            while (m_lst_loai_hd_da_add.Items.Count > 0 && m_lst_loai_hd_da_add.SelectedItem != null)
            {
                ListItem selectedItem = m_lst_loai_hd_da_add.SelectedItem;
                selectedItem.Selected = false;
                m_lst_loai_hd_chua_add.Items.Add(selectedItem);
                m_lst_loai_hd_da_add.Items.Remove(selectedItem);
            }

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }

    }
    protected void m_cmd_left_all_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            foreach (ListItem ltTemp in this.m_lst_loai_hd_da_add.Items)
            {
                this.m_lst_loai_hd_chua_add.Items.Add(ltTemp);
            }
            this.m_lst_loai_hd_da_add.Items.Clear();


        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cmd_cap_nhat_Click(object sender, EventArgs e)
    {
        try
        {
            m_lbl_mess.Text = "";
            update_dv_quan_ly_loai_hd();

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cbo_user_group_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            load_lst_loai_hd_chua_add();
            load_lst_loai_hd_da_add();

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
}